package com.my.simulation;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.my.drinkinggame.Main;
import com.my.drinkinggame.StaticSprites;
import com.my.simulation.Card.CardType;

public class Simulation {
	public Main main;
	boolean available;
	public File currentDirectory;
	public File currentFile;
	public ArrayList<Card> cardList;
	public FileHandle fileHandle;
	public boolean giveTake, myOwnCards, nerdFact, neverHaveI, standard, rolePlaying;
	
	public Simulation(Main m){
		main = m;
		available = false;
		cardList = new ArrayList<Card>();
	}

	public void reset(){
		cardList.clear();
	}

	public void initiateCards(){
		if(standard){
			System.out.println("Standard loaded!");
			fileHandle = Gdx.files.internal("data/cards/standard");
			String allCards = "";
			allCards = fileHandle.readString();
			String[] cards = allCards.split("Card");
			for(int i = 0; i< cards.length; i++){
				String[] temp = cards[i].split("#");
				Card newCard = new Card(CardType.REGULAR);
				for(int b = 0; b < temp.length; b++){
					if(b == 0){
						newCard.setHeader(temp[0]);
					}
					if(b == 1){
						newCard.setText(temp[1]);
					}		
				}
				cardList.add(newCard);
			}
		}
		
		if(giveTake){
			System.out.println("Give/Take loaded!");
			fileHandle = Gdx.files.internal("data/cards/givetake");
			String allCards = "";
			allCards = fileHandle.readString();
			String[] cards = allCards.split("Card");
			for(int i = 0; i< cards.length; i++){
				String[] temp = cards[i].split("#");
				Card newCard = new Card(CardType.REGULAR);
				for(int b = 0; b < temp.length; b++){
					if(b == 0){
						newCard.setHeader(temp[0]);
					}
					if(b == 1){
						newCard.setText(temp[1]);
					}		
				}
				cardList.add(newCard);
			}
		}
		
		if(nerdFact){
			System.out.println("NerdFacts loaded!");
			fileHandle = Gdx.files.internal("data/cards/nerdfacts");
			String allCards = "";
			allCards = fileHandle.readString();
			String[] cards = allCards.split("Card");
			for(int i = 0; i< cards.length; i++){
				String[] temp = cards[i].split("#");
				Card newCard = new Card(CardType.REGULAR);
				for(int b = 0; b < temp.length; b++){
					if(b == 0){
						newCard.setHeader(temp[0]);
					}
					if(b == 1){
						newCard.setText(temp[1]);
					}		
				}
				cardList.add(newCard);
			}
		}
		if(neverHaveI){
			System.out.println("Never have I... loaded!");
			fileHandle = Gdx.files.internal("data/cards/neverhavei");
			String allCards = "";
			allCards = fileHandle.readString();
			String[] cards = allCards.split("Card");
			for(int i = 0; i< cards.length; i++){
				String[] temp = cards[i].split("#");
				Card newCard = new Card(CardType.REGULAR);
				for(int b = 0; b < temp.length; b++){
					if(b == 0){
						newCard.setHeader(temp[0]);
					}
					if(b == 1){
						newCard.setText(temp[1]);
					}		
				}
				cardList.add(newCard);
			}
		}
		if(rolePlaying){
			System.out.println("Role-playing loaded!");
			fileHandle = Gdx.files.internal("data/cards/roleplaying");
			String allCards = "";
			allCards = fileHandle.readString();
			String[] cards = allCards.split("Card");
			for(int i = 0; i< cards.length; i++){
				String[] temp = cards[i].split("#");
				Card newCard = new Card(CardType.REGULAR);
				for(int b = 0; b < temp.length; b++){
					if(b == 0){
						newCard.setHeader(temp[0]);
					}
					if(b == 1){
						newCard.setText(temp[1]);
					}		
				}
				cardList.add(newCard);
			}
		}
		if (Gdx.files.isExternalStorageAvailable()) {
			if (Gdx.files.external("HHObeergame").exists() && Gdx.files.external("HHObeergame").isDirectory()) {
				available = true;
				currentDirectory = Gdx.files.external("HHObeergame").file();
			} else {
				Gdx.files.external("HHObeergame").file().mkdir();
				available = true;
				currentDirectory = Gdx.files.external("HHObeergame").file();
			}
		} else {
			available = false;
		}
		if(available && myOwnCards){
			System.out.println("Custom Cards loaded!");
			for (File f : currentDirectory.listFiles()) {
				System.out.println("Number of files: " + currentDirectory.listFiles().length);
				System.out.println(f.getName());
				if (f.isDirectory() || f.getName().endsWith(".HHO")) {
					String externalFile = (new FileHandle(f).readString());
					String[] temp = externalFile.split("#");
					Card newCard = new Card(CardType.REGULAR);
					for(int b = 0; b < temp.length; b++){
						if(b == 0){
							newCard.setHeader(temp[0]);
						}
						if(b == 1){
							newCard.setText(temp[1]);
						}		
					}
					if(newCard.header != null && newCard.text != null){
						cardList.add(newCard);	
					}
				}
			}
		}
	}
}
